在返回方法中使用导入函数(无效的钩子调用,react js)

您所在的位置:网站首页 return 只能在函数内部使用 在返回方法中使用导入函数(无效的钩子调用,react js)

在返回方法中使用导入函数(无效的钩子调用,react js)

2023-05-19 15:37| 来源: 网络整理| 查看: 265

伙计们,

我有一个可重用的组件,它通过将键名作为字符串或绑定的 var 将键翻译成一种选择的语言。

通常我为此使用标签,但由于不同的原因,我将当前翻译切换/替换为 {t('...')}。

这是组件的代码:

import React from 'react';

import { useTranslation as defaultTranslation } from 'react-i18next';

import i18next from 'i18next';

export const useTranslation = (ns = 'common', options) => {

  return defaultTranslation(ns, {

    useSuspense: false,

    ...options,

  });

};

export const withTranslation = (Component, ns, options) => {

  const TranslatedHOC = (props) => {

    const translationProps = useTranslation(ns, options);

    return ;

  };

  return TranslatedHOC;

};

export const getCurrentLanguage = () =>

  i18next.language || localStorage.getItem('language') || 'de-DE';

首先,我为使用的导入函数定义常量:

const {t} = useTranslation();

正常情况:在文件中导入我的组件,我想在其中使用它并在上面添加代码。

我的组件代码,我想在其中替换标签。

// Import React Table

import ReactTable from 'react-table';

import 'react-table/react-table.css';

import LocalizedText from '@components/i18n/LocalizedText';

class T extends React.Component {

  constructor(props) {

    super(props);

    this.state = {

      data: [],

      pages: null,

      loading: true,

    };

    this.fetchData = this.fetchData.bind(this);

  }

  fetchData(state, instance) {

    this.props.onFetchData(state, instance).then(() => {

      this.setState({

        loading: false,

      });

    });

  }

  render() {

    return (

     



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3